<?php
/*首页数据*/
namespace Admin\Model;
use Think\Model;
class CreditModel extends Model {
    public function getCredit($params) {
        $fileName="UserRecordJson/".$params['account_mobile'].'.json';
        $result=ossGetObject($fileName);
        
        if(!$result){
            $url="https://api.tongdun.cn/bodyguard/apply/v4?partner_code=bbtqc&partner_key=1660d9d859e04519a704ca531e9aaff9&app_name=bbt_web";
            $post_data = '';
            foreach($params as $k => $v) {
                $post_data .= $k.'='.urlencode($v).'&';
            }
            $result=$this->vpost($url,$post_data);
            $result_data=json_decode($result);
            if($result_data->success && !$result_data->reason_code){
                $fileName="UserRecordJson/".$params['account_mobile'].'.json';
                ossPutObject($fileName,$result);
            }
        }
        //获取征信信息
        $result_data=json_decode($result);
        if($result_data->success){
            $data['credit']['data']=$this->checkCredit($result);
            $data['credit']['code']=1;
        }else{
            $data['credit']['code']=0;
        }
       // 获取通讯录信息
       $data['contacts'] = $this->checkContacts($params['account_mobile']);
       $data['sms'] = $this->checkSms($params['account_mobile']);
       return  $data;
    }
    public function checkCredit($result){
        $check_str=[
            '身份证归属地位于高风险较为集中地区',
            '身份证命中法院失信名单',
            '身份证命中犯罪通缉名单',
            '身份证命中法院执行名单',
            '身份证命中信贷逾期名单',
            '身份证命中高风险关注名单',
            '身份证命中车辆租赁违约名单',
            '身份证命中法院结案名单',
            '身份证_姓名命中信贷逾期模糊名单',
            '身份证_姓名命中法院失信模糊名单',
            '身份证_姓名命中法院执行模糊名单',
            '身份证_姓名命中法院结案模糊名单',
            '身份证命中欠款公司法人代表名单',
            '身份证命中故意违章乘车名单',
            '身份证命中信贷逾期后还款名单',
            '手机号命中虚假号码库',
            '手机号命中诈骗骚扰库',
            '手机号命中高风险关注名单',
            '手机号命中信贷逾期名单',
            '手机号命中车辆租赁违约名单',
            '3个月内身份证关联多个申请信息',
            '3个月内申请信息关联多个身份证',
            '3个月内银行卡_姓名关联多个身份证',
            '3个月内申请人在多个平台申请借款',
            '3个月内申请人在多个平台被放款_不包含本合作方',
            '申请人信息命中中风险关注名单',
            '申请人信息命中低风险关注名单'
        ];
        
        $returnData=[];
        
        if($result){
            $data=json_decode($result);
            $returnData['workCheck']=-1;
            if($data->result_desc && $data->result_desc->AUTHENTICATION_INFOQUERY && $data->result_desc->AUTHENTICATION_INFOQUERY->WorkAddressCheck){
                 $returnData['workCheck']=$data->result_desc->AUTHENTICATION_INFOQUERY->WorkAddressCheck->work_address_consistence;
            }
            $returnData['homeCheck']=-1;
            if($data->result_desc && $data->result_desc->AUTHENTICATION_INFOQUERY && $data->result_desc->AUTHENTICATION_INFOQUERY->FamilyAddressCheck){
                 $returnData['homeCheck']=$data->result_desc->AUTHENTICATION_INFOQUERY->FamilyAddressCheck->home_address_consistence;
            }
            if($data->result_desc && $data->result_desc->AUTHENTICATION_INFOQUERY && $data->result_desc->AUTHENTICATION_INFOQUERY->MobileDuration){
                if($data->result_desc->AUTHENTICATION_INFOQUERY->MobileDuration->mobile_online_time_info){
                    $returnData['monbileOnline']=$data->result_desc->AUTHENTICATION_INFOQUERY->MobileDuration->mobile_online_time_info;
                }
            }
            $returnData['record']=[];
            if($data->result_desc && $data->result_desc->ANTIFRAUD && $data->result_desc->ANTIFRAUD->risk_items){
                $risk_items=$data->result_desc->ANTIFRAUD->risk_items;
                foreach($risk_items as $val){
                    if(in_array($val->risk_name,$check_str)){
                        $returnData['record'][]=$val->risk_name;
                    }
                }
            }   
        }
        return  $returnData;
    }
    public function checkContacts($tel)
    {
        $fileName="UserContactJson/".$tel.'.json';
        $result=ossGetObject($fileName);
        $returnData=[];
        if($result){
            $data=json_decode($result);
            $returnData['data']=[];
            foreach($data as $val){
                $nameObj=$val->name;
                $phoneObj=$val->phoneNumbers;
                if($phoneObj  && $nameObj){
                    $name=($nameObj->familyName?$nameObj->familyName:'').($nameObj->givenName?$nameObj->givenName:'');
                    $phone_arr=[];
                    foreach($phoneObj as $val){
                        $phone_arr[]=$val->value;
                    }
                    $arr=[];
                    $arr["name"]=$name;
                    $arr["phone"]=$phone_arr;
                    $returnData['data'][]=$arr;
                }
                
            }
            $returnData['code']=1;
        }else{
            $returnData['code']=0;
        }
        return $returnData;
    }

    public function checkSms($tel){
        $check_str=['还款','欠款','借款','欠钱','还钱','借钱','催款'];
        $fileName="UserSmsJson/".$tel.'.json';
        $result=ossGetObject($fileName);
        $returnData=[];
        if($result){
            $data=json_decode($result);
            $returnData['data']=[];
            foreach($data as $val){
               foreach($check_str as $v){
                   if(strpos($val->BODY,$v) !== false){
                       $arr=[];
                       $arr['tel']=$val->ADDRESS;
                       $arr['content']=$val->BODY;
                       $arr['time']=$val->DATE;
                       $returnData["data"][]=$arr;
                       break 1;
                   }
               } 
            }
            $returnData['code']=1;
        }else{
            $returnData['code']=0;
        }
        return $returnData;
    }

    /**
    * 模拟post获取内容函数
    */
    public function vpost($url, $data) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

        // 5秒超时
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HEADER, 0);

        $res = curl_exec($curl);
        if (curl_errno($curl)) {
            $res = curl_error($curl);
        }
        curl_close($curl);
        return $res;
    }
}
?>